package com.kaishu.misp.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.kaishu.misp.action.BaseAction;
import com.kaishu.misp.action.Json;
import com.kaishu.misp.utils.ExceptionUtil;
import com.kaishu.misp.entity.Customer;
import com.kaishu.misp.service.CustomerService;
/**
 * 客户控制器 CustomerAction
 *
 */
@Controller
public class CustomerAction extends BaseAction implements ModelDriven<Customer> {
	
	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(CustomerAction.class);
	@Autowired
	private CustomerService customerService;
	public Customer customer = new Customer();
	/**
	 * 得到数据列表
	 */
	@Action("customerGrid")
	public void grid(){
		int total = customerService.getTotal();
		System.out.print("Total: " + total);
		List<Customer> list = customerService.findAll(page, rows, sort, order);
		writeJson(total,list);
	}
	/**
	 * 添加操作
	 */
	@Action("customerAdd")
	public void add(){
			Json result = new Json();
			try {
				customerService.save(customer);
				result.setSuccess(true);
				result.setMsg("添加客户成功!!!");					
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加客户失败q");	
				System.out.println(ExceptionUtil.getExceptionMessage(e));		
			}
			System.out.println(customer.getAddress());
			System.out.println(customer.getCustomername());
			System.out.println(customer.getCustomerno());
			System.out.println(customer.getTelephone());
			System.out.println(customer.getId());
			writeJson(result);
	}
	/**
	 * 修改操作
	 */
	@Action("customerEdit")
	public void edit() {
			Json result = new Json();
			try {
				customerService.update(customer);
				result.setSuccess(true);
				result.setMsg("修改客户成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加客户失败");
			}
			writeJson(result);
	}
	/**
	 * 删除操作
	 */
	@Action("customerRemove")
	public void remove() {
			Json result = new Json();
			try {
				customerService.delete(customer.getId());
				result.setSuccess(true);
				result.setMsg("删除客户成功");
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("删除客户失败");
			}
			writeJson(result);
	}
	@Override
	public Customer getModel() {
		return customer;
	}	
}